import React from 'react';
import '../../static/game.css';

function Square(props) {
    return (<button className="square" onClick={props.onClick}>{props.value}</button>)
}

class Board extends React.Component {
    renderSquare(i) {
        return (
            <Square
                key={i}
                value={this.props.squares[i]}
                onClick={this.props.onClick.bind(this, i)}
            />
        );
    }

    render() {
        let _this = this;
        return (
            <div>{
                [0, 1, 2].map((rowIndex) =>
                    <div className="board-row" key={rowIndex}>{
                        [0, 1, 2].map((colIndex) =>
                            _this.renderSquare(rowIndex * 3 + colIndex)
                        )}
                    </div>
                )}
            </div>
        );
    }
}

class Game extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            history: [
                {squares: Array(9).fill(null)}
            ],
            xIsNext: true
        }
    }

    clickHandler(i) {
        const history = this.state.history;
        const current = history[history.length - 1];
        const squares = current.squares.slice();
        if (Game.calculateWinner(squares) || squares[i]) {
            return;
        }
        squares[i] = this.state.xIsNext ? 'X' : 'O';
        this.setState({
            history: history.concat([{squares: squares}]),
            xIsNext: !this.state.xIsNext
        });
    }

    resetHandler() {
        if (window.confirm("确定重新开始吗？")) {
            this.setState({
                history: [
                    {squares: Array(9).fill(null)}
                ],
                xIsNext: true
            });
        }
    }

    backHandler() {
        if (window.confirm("确定重新开始吗？")) {
            const history = this.state.history.slice();

            this.setState({
                history: history.slice(0, history.length - 1),
                xIsNext: !this.state.xIsNext
            });
        }
    }

    static calculateWinner(squares) {
        const lines = [
            [0, 1, 2],
            [3, 4, 5],
            [6, 7, 8],
            [0, 3, 6],
            [1, 4, 7],
            [2, 5, 8],
            [0, 4, 8],
            [2, 4, 6],
        ];
        for (let i = 0; i < lines.length; i++) {
            const [a, b, c] = lines[i];
            if (squares[a] && squares[a] === squares[b] && squares[a] === squares[c]) {
                return squares[a];
            }
        }
        return null;
    }
    render() {
        const history = this.state.history.slice();
        const current = history[history.length - 1];
        const winner = Game.calculateWinner(current.squares);
        const fulfill = !current.squares.includes(null);
        let status = null;
        if (winner) {
            status = 'Winner is: ' + winner;
        } else if (fulfill) {
            status = 'Fulfill ! Please restart ! ';
        } else {
            status = 'Next player: ' + (this.state.xIsNext ? 'X' : 'O');
        }
        return (
            <div className="game">
                <div className="game-board">
                    <Board
                        squares={current.squares}
                        onClick={(i) => this.clickHandler(i)}
                    />
                </div>
                <div className="game-info">
                    <div className="status">{status}</div>
                    <button onClick={() => {
                        this.resetHandler()
                    }}>重新开始
                    </button>
                    <br/>
                    <br/>
                    <button onClick={() => {
                        this.backHandler()
                    }}>返回上一步
                    </button>
                </div>
            </div>
        );
    }
}

export default Game